#include <iostream>
#include <string.h>
#include <zconf.h>
int main()
{
    pid_t result;
    int r_num;
    int pipe_fd[2];
    char buf_r[100], buf_w[100];
    memset(buf_r, 0, sizeof(buf_r));
    if (pipe(pipe_fd) < 0) //父进程调用pipe函数，创建一个管道
    {
        printf("创建管道失败\n");
        return -1;
    }
    result = fork();
    if (result < 0)
    {
        perror("创建子进程失败\n");
        exit(0);
    }
    else if (result == 0) // 子进程代码
    {
        close(pipe_fd[1]);
        if (r_num == read(pipe_fd[0], buf_r, 100) > 0)
        {
            printf("子进程从管道读取 %d 个字符，读取字符为：%s\n", r_num, buf_r);
            close(pipe_fd[0]);
            exit(0);
        }
    }
    else // 父进程运行代码
    {
        close(pipe_fd[0]);
        printf("请从键盘输入写入管道的字符串：\n");
        scanf("%s", buf_w);
        if (write(pipe_fd[1], buf_w, strlen(buf_w)) != 1 )
            printf("父进程向管道写入 :%s\n",buf_w);
        close(pipe_fd[1]);
        waitpid(result, nullptr, 0); //调用waitpid，阻塞父进程，等待子进程退出
        exit(0);
    }
    std::cout << "Hello, World!" << std::endl;
    return 0;
}